package org.insia.smartposse.dao;

import java.util.ArrayList;
import java.util.List;

import org.insia.smartposse.config.DAOFactory;
import org.insia.smartposse.idao.IProjectDAO;
import org.insia.smartposse.mail.SmtpMail;
import org.insia.smartposse.metier.Freelance;
import org.insia.smartposse.metier.Project;
import org.insia.smartposse.metier.User;
import org.insia.smartposse.metier.webservice.LightCategoryTheme;
import org.insia.smartposse.metier.webservice.LightProject;
import org.insia.smartposse.metier.webservice.LightProjectSkill;
/**
 * DAO for Project class.
 * @author sok hout
 *
 */
public class ProjectDAO 
extends GenericDAO<Project, Long> 
implements IProjectDAO {
	/**
	 * Empty constructor
	 *
	 */
	public ProjectDAO() {
		type = Project.class;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.insia.smartposse.idao.IProjectDAO#getLast()
	 */
	public Project getLast() {
		Project rst = null;

		Integer lastId = (Integer) _SESSION.createQuery(
				"select max(id) Project as proj").uniqueResult();
		rst = (Project) _SESSION.createQuery(
				"select * from Project as proj where proj.id = ?").setEntity(0,
				lastId.longValue()).uniqueResult();

		return rst;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.insia.smartposse.idao.IProjectDAO#getFrom(long)
	 */
	@SuppressWarnings("unchecked")
	public List<Project> getFrom(long id) {
		List<Project> lproj = new ArrayList<Project>();
		User usr = null;
		try {
			usr = DAOFactory.getUserDAO().get(id);
			if (usr != null)
				lproj = (List<Project>) _SESSION.createQuery(
						"from Project as proj where proj.creator is null")
						.setEntity(0, usr).list();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return lproj;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.insia.smartposse.idao.IProjectDAO#createLightProject(org.insia.smartposse.metier.webservice.LightProject)
	 */
	public LightProject createLightProject(LightProject lproject) {
		try {
			//ResourceBundle message = ResourceBundle.getBundle("org.insia.smartposse.struts.ApplicationRessources");
			Project project = new Project();
			project.setBudget(lproject.getBudget());
			project.setCreator(DAOFactory.getUserDAO().get(
					lproject.getCreator()));
			project.setDescription(lproject.getDescription());
			project.setStart_date(lproject.getStart_date());
			project.setEnd_date(lproject.getEnd_date());
			project.setUrl(lproject.getUrl());
			project.setTitle(lproject.getTitle());
			for (LightCategoryTheme lcategoryTheme : lproject
					.getProjectCategory()) {
				project.getProjectCategory().add(
						DAOFactory.getCategoryThemeDAO().get(
								lcategoryTheme.getId()));
			}
			lproject.setId(DAOFactory.getProjectDAO().create(project));
			
			lproject.setProjectSkills(DAOFactory.getProjectSkillDAO()
					.createLightProjectSkill(lproject.getProjectSkills()));

			List<Long> skillz_id = new ArrayList<Long>();
			
			for (LightProjectSkill projectSkill : lproject.getProjectSkills()) {
				skillz_id.add(projectSkill.getSkill());
			}
			
			List<Freelance> freelances = DAOFactory.getUserDAO().getFromSkill(skillz_id);
			
			for (Freelance freelance : freelances) {
				SmtpMail mail = new SmtpMail(project.getCreator().getEmail(), new String[] {
					freelance.getEmail() },
					"smtp.free.fr");
			
				mail.Initialize("project.new.send.mail.title");
				mail.setText("project.new.send.mail.text", false);
				//mail.SendMail();
			}
		
		} catch (Exception e) {
			e.printStackTrace();
		}

		return lproject;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.insia.smartposse.idao.IProjectDAO#createLightProject(java.util.List)
	 */
	public List<LightProject> createLightProject(List<LightProject> projectSet) {
		List<LightProject> Lproject = new ArrayList<LightProject>();
		try {
			for (LightProject lproject : projectSet) {
				lproject = createLightProject(lproject);
				Lproject.add(lproject);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return Lproject;
	}

	/*
	 * (non-Javadoc)
	 * @see org.insia.smartposse.idao.IProjectDAO#updateLightProject(org.insia.smartposse.metier.webservice.LightProject)
	 */
	public LightProject updateLightProject(LightProject lproject) {
		try {
			Project project = DAOFactory.getProjectDAO().get(lproject.getId());;
			project.setBudget(lproject.getBudget());
			project.setCreator(DAOFactory.getUserDAO().get(
					lproject.getCreator()));
			project.setDescription(lproject.getDescription());
			project.setStart_date(lproject.getStart_date());
			project.setEnd_date(lproject.getEnd_date());
			project.setUrl(lproject.getUrl());
			project.setTitle(lproject.getTitle());
			for (LightCategoryTheme lcategoryTheme : lproject
					.getProjectCategory()) {
				project.getProjectCategory().add(
						DAOFactory.getCategoryThemeDAO().get(
								lcategoryTheme.getId()));
			}
			lproject.setProjectSkills(DAOFactory.getProjectSkillDAO()
					.updateLightProjectSkill(lproject.getProjectSkills()));
			DAOFactory.getProjectDAO().update(project);

		
		} catch (Exception e) {
			e.printStackTrace();
		}

		return lproject;
	}

	/*
	 * (non-Javadoc)
	 * @see org.insia.smartposse.idao.IProjectDAO#updateLightProject(java.util.List)
	 */
	public List<LightProject> updateLightProject(List<LightProject> projectSet) {
		List<LightProject> Lproject = new ArrayList<LightProject>();
		try {
			for (LightProject lproject : projectSet) {
				lproject = updateLightProject(lproject);
				Lproject.add(lproject);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return Lproject;
	}
}
